package com.blackline.auto.Report;

import static org.junit.Assert.*;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;

import com.blackline.declareUI.UI;
import com.blackline.parameters.Parameters;

/*
 * 
 * 
 * test case Number in test rail  C46507
 * 
 * */





public class ScheduleAll_UPSReports {
	private WebDriver driver;
	private StringBuffer verificationErrors = new StringBuffer();
	private static Logger logger=Logger.getLogger(Reports_smoketest.class);
	UI UI=new UI();
	Parameters parameters;
	int i=0;
	String NameofReport;
	@Before
	public void setUp() throws Exception {
		
		
		FirefoxProfile profile=new FirefoxProfile();
		profile.setEnableNativeEvents(true);
		parameters=new Parameters("parameters.xml");
		driver=new FirefoxDriver();
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		driver.manage().window().maximize();
		UI.declareAllUI(driver);
		
	}

	

	@Test
	public void  test() throws Exception  {
		int sleepTime = 5000;
		//driver.get(pa.urlDev2);
		//to get the current date and set its as start date  also append it in name .
		Date now=new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("MM/dd/yyyy");
		Calendar cal=Calendar.getInstance();
		cal.setTime(now);
		cal.add(Calendar.DAY_OF_MONTH, 1);
		String startDate=sdf.format(cal.getTime());
		logger.info(startDate);

		// current time  to append it in name
		SimpleDateFormat sdf1=new SimpleDateFormat("hh:mm");
		Calendar cal1=Calendar.getInstance();
		cal1.setTime(now);
		cal1.add(Calendar.DAY_OF_MONTH, 1);
		String startTime=sdf1.format(cal1.getTime());
		logger.info(startTime);
		
		
		SimpleDateFormat sdf2=new SimpleDateFormat("yyyy");
		String years=sdf2.format(cal.getTime());
		String endDate="12/31/"+years;
		logger.info(endDate);
		
		
		logger.info("You've selected site: https://t2.blacklineondemand.com/Test3");
		UI.LogonPage.Login(driver, "https://t2.blacklineondemand.com/Test2", parameters.getLogonID("admin"), parameters.getPassword("admin"));
		//Thread.sleep(3000);
		  UI.NavigationMenu.REPORTS.click();
		  UI.Reports.Enhanced_Reporting_link.click();
		  Thread.sleep(20000);
		  
		  //ups Reports 
		  
		  UI.Reporting.AddIcon_UPSReports.click();
		  UI.Reporting.UPS_nodeInLeftTree.click();
		  // to go through all the reports of ups 
		  Thread.sleep(20000);
		  int count=0;
		 for (i=1;i<22;i++)
		 {
							
			 Thread.sleep(2000);
						 if(i<9)
							  NameofReport="0"+i;
						 		/*UI.Reporting.Name_text_field.sendKeys("0"+i);*/
						 else if(i!=9) 
					 		  NameofReport=i+" ";
						  else
						  {   
									  if(count<=3)
										  {
										  
										  			
												   if(count==0)
													   NameofReport="0"+i+" ";
												   else if(count==1)
													   NameofReport="0"+i+"A";
												   else  if(count==2)
													   NameofReport="0"+i+"B";
												   else if(count==3)
												   { 
													   
													   NameofReport="0"+i+"C";
												   }   
												   i--;
												   if(count==3)
													   i++;
												   count++;
												   
										  }
						
						  }
						  
						  		UI.Reporting.Name_text_field.clear();
						 		UI.Reporting.Name_text_field.sendKeys(NameofReport);
						 		UI.Reporting.Refresh_button.click();
						 		
						  String schedulename = UI.Reporting.Schedule_Name_SearchedResult.getText();
						 logger.info(schedulename);
						  
						 
						 
						 assertNotNull(UI.Reporting.There_are_no_records_text);
						 // enter the scheduling 
						 UI.Reporting.Schedule_linkOfSearchedResult.click();
						 
						 
						 UI.SchedulePrompts.Schedule_Name_Text.clear();
						 UI.SchedulePrompts.Schedule_Name_Text.sendKeys("UPS"+NameofReport+startDate+startTime);
						 // edit of start date
						 UI.SchedulePrompts.Schedule_Start_Date_Text.clear();
						 UI.SchedulePrompts.Schedule_Start_Date_Text.sendKeys(startDate);
						 //editing end date 
						 UI.SchedulePrompts.Schedule_End_Date_Text.clear();
						 UI.SchedulePrompts.Schedule_End_Date_Text.sendKeys(endDate);
						 
						 UI.SchedulePrompts.Schedule_Report_button.click();
						 
						 Thread.sleep(20000);
						// Thread.sleep(25000);
						 // missed the box and click on ohk .....
					
						 String runRes=UI.Alert_inReporting.Message_Text.getText();
						 
						
							if(runRes.contains("has been created"))
							  {
								  logger.info("[Pass!] "+runRes);
								  UI.Alert_inReporting.OK_button.click();
							  }
							
							  else{
								  logger.error("[Fail!] "+runRes);
								  verificationErrors.append("[Fail!] "+runRes);
								  fail("Schedule failed: "+runRes);
							  }
						 
				
						 
		 			}
		 
		/* to verify if reports are scheduled properly or not 
		 
		 *
		 *
		 *
		 *
		 *
		 */
		 UI.Reporting.My_scheduled_Report_nodeInLeftTree.click();
		  Thread.sleep(20000);
		  // reaching till scheduled report 
		  UI.Reporting.Name_text_field.clear();
		  UI.Reporting.Name_text_field.sendKeys(startDate+startTime);
		  
		  logger.info(startDate+startTime);
		  
		  
		  UI.Reporting.Refresh_button.click();
		  String schedulename = UI.Reporting.Schedule_Name_SearchedResult.getText();
		 logger.info(schedulename);
		  assertNotNull(UI.Reporting.There_are_no_records_text	);
		  Thread.sleep(2000);
		  
		  String result= UI.Reporting.Number_of_results.getText();
		  
		  
		  
		  // number of reports for UPS is constant -"25"
		  
		  if(result.contains("24"))
		  {
			  logger.info("[Pass!] "+result);
			  UI.Alert_inReporting.OK_button.click();
		  }
		
		  else{
			  logger.error("[Fail!] "+result);
			  verificationErrors.append("[Fail!] "+result);
			  fail("Schedule failed: "+result);
		  }
	 

	  Thread.sleep(2000);
	  
	// delete(startDate+startTime);
	}

	
	
	public void delete( String report_name ) throws Exception{
		
		 UI.Reporting.My_scheduled_Report_nodeInLeftTree.click();
		  Thread.sleep(20000);
		  // reaching till scheduled report 
		  UI.Reporting.Name_text_field.clear();
		  UI.Reporting.Name_text_field.sendKeys(report_name);
		 
		  Thread.sleep(2000);
		  
		  
		  for(i=0;i<24;i++)
		  {
			  
				 
			  Thread.sleep(2000);
			  UI.Reporting.Delete_linkOfSearchedScheduledReport.click();
		  String runRes=UI.Alert_inReporting.Message_Text.getText();
		  
			if(runRes.contains("you want to delete it"))
			  {
				  logger.info("[Pass!] "+runRes);
				  UI.Alert_inReporting.OK_button.click();
			  }
			
			  else{
				  logger.error("[Fail!] "+runRes);
				  verificationErrors.append("[Fail!] "+runRes);
				  fail("Schedule failed: "+runRes);
			  }
		  }

		 
	}
		
		
		
	
	
	
	@After
	public void tearDown() throws Exception {
		
		String verificationErrorString = verificationErrors.toString();
		if (!"".equals(verificationErrorString)) {
			fail(verificationErrorString);
		}
		
		
		
	}
}
